package com.android.qjpmc.data_module;

import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.bean.SiteStationBean;
import com.android.qjpmc.bean.StationDataBean;

import java.util.List;

//电站类 luzhene 2021年11月24日
public class PwrStation {

    StationDataBean m_DeviceBeanListCntn = null;
    List<PwrDeviceBean> m_DeviceBeanList = null;

    public StationDataBean GetDeviceListCntnBean()
    {
        return m_DeviceBeanListCntn;
    }

    public StationDataBean CreateDeviceListCntnBean(List<PwrDeviceBean> list)
    {
        m_DeviceBeanListCntn = new StationDataBean();
        if (m_DeviceBeanListCntn != null)
        {
            m_DeviceBeanListCntn.stationGroupList = list;
        }
        return m_DeviceBeanListCntn;
    }

    SiteStationBean m_station_bean = null;

    public int getM_ID() {
        return m_ID;
    }

    public String getM_Name() {
        return m_Name;
    }

    private int m_ID;
    private String m_Name;
    private PwrStation m_NextNode = null;
    private PwrStation m_PreNode = null;

    private PwrDevice m_CurrentDeviceNode = null;


    private PwrDeviceSet m_DeviceSet = new PwrDeviceSet();

    private int m_AllDeviceBeLoaded = 0;

    public void SetAllDeviceBeLoadedMark(int Mark)
    {
        m_AllDeviceBeLoaded = Mark;
    }

    public int IsAllDeviceBeLoaded()
    {
        return m_AllDeviceBeLoaded;
    }

    public int AppendANode(PwrStation Node) {
        PwrStation NextNode = GetNext();
        if (Node == null) {
            return 1;
        }
        Node.SetNext(NextNode);
        SetNext(Node);
        if (NextNode != null) {
            NextNode.SetPre(Node);
        }
        return 0;
    }

    public void SetID(int ID) {
        m_ID = ID;
    }

    public void SetName(String Name) {
        m_Name = Name;
    }

    public int IsMe(int ID) {
        if (m_ID == ID) {
            return 1;
        }
        return 0;
    }

    public PwrStation GetNext() {
        return m_NextNode;
    }

    public PwrStation GetPre() {
        return m_PreNode;
    }

    public void SetNext(PwrStation Station) {
        m_NextNode = Station;
    }

    public void SetPre(PwrStation Station) {
        m_PreNode = Station;
    }

    public PwrDeviceSet GetPwrDeviceSet() {
        return m_DeviceSet;
    }


    /*
     *
     * 增加设备节点到设备队列中
     * Device：设备节点
     *
     * */
    int AddDevice(PwrDevice Device) {
        PwrDevice TheDevice = null;
        PwrDeviceSet Set = GetPwrDeviceSet();
        if (Set == null) {
            return 1;
        }
        TheDevice = Set.GetPwrDevice(Device.GetID());
        if (TheDevice == null) {
            Set.AppendADevice(Device);
        } else {
            return 1;
        }
        return 0;
    }

    /*
     *
     * 获取设备节点
     * DeviceID：设备ID
     * */
    public PwrDevice GetPwrDevice(int DeviceID) {
        PwrDeviceSet Set = GetPwrDeviceSet();
        if (Set == null) {
            return null;
        }
        return Set.GetPwrDevice(DeviceID);
    }

    //根据索引获取设备
    public PwrDevice GetPwrDeviceByIndex(int idx) {
        PwrDeviceSet Set = GetPwrDeviceSet();
        if (Set == null) {
            return null;
        }
        return Set.GetPwrDeviceByIndex(idx);
    }

    public AnalogDataItem GetAnalogDataItem(int device_index, int value_index) {
        PwrDevice device = GetPwrDeviceByIndex(device_index);
        if (device == null) {
            return null;
        }
        return device.GetAnalogDataItemByIndex(value_index);
    }

    ;

    /*
     *
     * 移除设备
     * DeviceID:设备ID
     *
     * */
    public int RemoveDevice(int DeviceID) {
        PwrDevice TheDevice = null;
        PwrDeviceSet Set = GetPwrDeviceSet();
        if (Set == null) {
            return 1;
        }
        return Set.RemoveDevice(DeviceID);
    }

    //析构函数
    public void finalize() {
        if (m_DeviceSet != null) {
            m_DeviceSet = null;
        }
        ;
    }

    //获取第一个设备
    public PwrDevice GetFirstDevice() {
        PwrDeviceSet Set = GetPwrDeviceSet();
        if (Set == null) {
            return null;
        }
        return Set.GetHead();
    }

    public void SetCurrentPwrDeviceNode(PwrDevice Device) {
        m_CurrentDeviceNode = Device;
    }

    public PwrDevice GetCurrentPwrDeviceNode() {
        return m_CurrentDeviceNode;
    }

    void SetStationBean(SiteStationBean bean) {
        m_station_bean = bean;
        if (bean != null) {
            m_ID = Integer.valueOf(bean.stationId);
            m_Name = bean.company_name;
        }
    }

    public SiteStationBean GetStationBean() {
        return m_station_bean;
    }

    public List<PwrDeviceBean> GetDeviceBeanList() {
        if (m_DeviceBeanList != null)
        {
            return m_DeviceBeanList;
        }
        PwrDeviceSet Set = GetPwrDeviceSet();
        if (Set == null) {
            return null;
        }
        m_DeviceBeanList = Set.GetDeviceBeanList();
        return m_DeviceBeanList;
    }



    public int CreateAllPowerDeviceOfAStation(List<PwrDeviceBean> Beans) {
        PwrDeviceSet Set = GetPwrDeviceSet();
        if (Set == null) {
            return 1;
        }
        return Set.CreateAllPowerDeviceOfAStation(Beans);
    }

    public int GetDeviceCount()
    {
        PwrDeviceSet Set = GetPwrDeviceSet();
        if (Set == null) {
            return 1;
        }
        return Set.GetCount();
    }

    List<PwrDeviceBean> GetDeviceBeans()
    {
        PwrDeviceSet Set = GetPwrDeviceSet();
        if (Set == null) {
            return null;
        }
        return Set.GetDeviceBeanList();
    }
    //是否所有测点被加载?
    public int IsAllPointBeLoaded()
    {
        if (IsAllDeviceBeLoaded() == 0)
        {
            return 0;
        }
        PwrDevice device = GetFirstDevice();
        while (device != null)
        {
            if (device.IsPointLoaded() != 1)
            {
                return 0;
            }
        }
        return 1;
    }
    public String GetID()
    {
        return Integer.toString(getM_ID());
    }



}
